﻿using System.Reflection;
using System.Windows.Forms;

namespace CodeSnippet.Helpers
{
    /// <summary>
    /// Helper class to manipulate the <see cref="PropertyGrid"/> control.
    /// </summary>
    internal static class PropertyGridHelper
    {
        #region Operations
        /// <summary>
        /// Resizes the property grid columns.
        /// </summary>
        /// <param name="pg">The <see cref="PropertyGrid"/> instance.</param>
        /// <param name="percent">The percent.</param>
        public static void ResizePropertyGridColumns(Control pg, int percent)
        {
            var type = pg.GetType();
            var fieldInfo = type.GetField("gridView", BindingFlags.NonPublic | BindingFlags.Instance);
            var gridViewRef = fieldInfo.GetValue(pg);
            var gridViewType = gridViewRef.GetType();
            var gridColWidth = (int)(pg.Width * ((float)percent / 100));
            var moveSplitterTo = gridViewType.GetMethod("MoveSplitterTo", BindingFlags.NonPublic | BindingFlags.Instance);
            moveSplitterTo.Invoke(gridViewRef, new object[] { gridColWidth });
        }
        #endregion Operations
    }
}
